package com.witmore.skyline.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.witmore.skyline.db.entity.AdminDO;
import com.witmore.skyline.db.entity.OrgDO;
import com.witmore.skyline.db.entity.ProjectBusinessRefDO;
import com.witmore.skyline.db.entity.ProjectDO;
import com.witmore.skyline.db.mapper.AdminMapper;
import com.witmore.skyline.db.mapper.OrgMapper;
import com.witmore.skyline.db.mapper.ProjectBusinessRefMapper;
import com.witmore.skyline.db.mapper.ProjectMapper;
import com.witmore.skyline.web.config.GlobalAccountSession;
import com.witmore.skyline.web.controller.enums.AdminTypeEnum;
import com.witmore.skyline.web.controller.enums.WitmoreErrorCode;
import com.witmore.skyline.web.controller.vo.param.AdminParamVO;
import com.witmore.skyline.web.controller.vo.param.PageParamVO;
import com.witmore.skyline.web.controller.vo.result.AccountSession;
import com.witmore.skyline.web.controller.vo.result.AdminResultVO;
import com.witmore.skyline.web.controller.vo.result.PageInfo;
import com.witmore.skyline.web.controller.vo.result.ResponsePageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 账号管理
 * Author: 豆子高
 * Date: 2025/2/27 14:26
 */
@RestController
@RequestMapping("api/admin/")
public class AdminController{

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private OrgMapper orgMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ProjectBusinessRefMapper projectBusinessRefMapper;

    /**
     * 管理员列表
     */
    @GetMapping(value = "adminList", name = "管理员列表")
    public ResponsePageResult<AdminResultVO> projectList(AdminParamVO adminParamVO) {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();
        PageParamVO pageParamVO = new PageParamVO(adminParamVO.getPage(), adminParamVO.getSize());
        adminParamVO.setPage(pageParamVO.getPage());
        adminParamVO.setSize(pageParamVO.getSize());
        Page<AdminDO> adminDOPage = adminMapper.selectPage(new Page<>(adminParamVO.getPage(), adminParamVO.getSize()),
                new LambdaQueryWrapper<AdminDO>()
                        .eq(AdminDO::getOrgId, accountInfo.getOrgId())
                        .eq(AdminDO::getIsDeleted, 0)
                        .eq(!ObjectUtils.isEmpty(adminParamVO.getAdminType()), AdminDO::getAdminType, adminParamVO.getAdminType())
                        .like(!ObjectUtils.isEmpty(adminParamVO.getAdminName()), AdminDO::getAdminName, adminParamVO.getAdminName())
        );
        if (adminDOPage == null) {
            return ResponsePageResult.fail(WitmoreErrorCode.ADMIN_GET_FAIL);
        }
        List<AdminDO> records = adminDOPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return ResponsePageResult.success(null,
                    new PageInfo(adminDOPage.getCurrent(), adminDOPage.getSize(), adminDOPage.getTotal(), adminDOPage.getPages()));
        }
        List<AdminResultVO> resultVOS = new ArrayList<>();
        records.forEach(adminDO -> {
            AdminResultVO build = AdminResultVO.builder()
                    .id(adminDO.getId())
                    .userName(adminDO.getUserName())
                    .adminName(adminDO.getAdminName())
                    .orgId(adminDO.getOrgId())
                    .adminType(adminDO.getAdminType())
                    .adminTypeName(AdminTypeEnum.getNameByCode(adminDO.getAdminType()))
                    .createTime(adminDO.getCreateTime())
                    .isDeleted(adminDO.getIsDeleted())
                    .build();
            OrgDO orgDO = orgMapper.selectById(adminDO.getOrgId());
            if (null == orgDO) {
                build.setOrgName("");
            } else {
                build.setOrgName(orgDO.getOrgName());
            }
            List<ProjectBusinessRefDO> projectBusinessRefDOS = projectBusinessRefMapper.selectList(new QueryWrapper<ProjectBusinessRefDO>()
                    .eq("org_id", accountInfo.getOrgId())
                    .eq("type", 3)
                    .eq("skyline_business_id", adminDO.getId())
            );
            if (CollectionUtils.isEmpty(projectBusinessRefDOS)) {
                build.setProjectNameList(new ArrayList<>());
            } else {
                List<Integer> projectIds = projectBusinessRefDOS.stream()
                        .map(ProjectBusinessRefDO::getProjectId)
                        .collect(Collectors.toList());
                List<ProjectDO> projectDOS = projectMapper.selectList(new QueryWrapper<ProjectDO>()
                        .in("id", projectIds));
                if (CollectionUtils.isEmpty(projectDOS)) {
                    build.setProjectNameList(new ArrayList<>());
                } else {
                    List<String> projectNames = projectDOS.stream()
                            .map(ProjectDO::getProjectName)
                            .collect(Collectors.toList());
                    build.setProjectNameList(projectNames);
                }
            }
            resultVOS.add(build);

        });
        return ResponsePageResult.success(resultVOS,
                new PageInfo(adminDOPage.getCurrent(), adminDOPage.getSize(), adminDOPage.getTotal(), adminDOPage.getPages()));
    }

}
